package com.wys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.common.BaseContext;
import com.wys.dao.ShoppingCartDao;
import com.wys.pojo.ShoppingCart;
import com.wys.result.R;
import com.wys.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @Override
    public R<ShoppingCart> add(ShoppingCart shoppingCart, HttpSession session) {
        //获取当前登陆的用户ID
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        String dishFlavor = shoppingCart.getDishFlavor();
        //判断当前添加的是菜品还是套餐
        if(dishId != null){
            //是菜品，判断数据表中该用户是否存在该菜品,口味是否相同
            wrapper.eq(ShoppingCart::getDishId,dishId);
            //wrapper.eq(dishFlavor != null,ShoppingCart::getDishFlavor,dishFlavor);
        }else {
            //是套餐，判断数据表中是否存在该套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cart = this.getOne(wrapper);
        //如果存在，直接数量加1
        if(cart != null){
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
            this.updateById(cart);
        }else {
            //不存在，则添加入数据表，并设置数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCart(HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = this.list(wrapper);
        return shoppingCarts;
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public void deletedShoppingCart(HttpSession session) {
        //当前登录用户
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        remove(wrapper);
    }
    /**
     * 购物车中菜品数量减一
     * @return
     */
    @Override
    public R<String> deletedWithOne(ShoppingCart shoppingCart,HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        wrapper.eq(dishId != null,ShoppingCart::getDishId,dishId);
        wrapper.eq(setmealId != null,ShoppingCart::getSetmealId,setmealId);
        ShoppingCart shopping = getOne(wrapper);
        if(shopping.getNumber() == 1){
            //数据表中只存在一份，则直接删除
            removeById(shopping);
        }else {
            //数据表中数据减一
            shopping.setNumber(shopping.getNumber()-1);
            updateById(shopping);
        }
        return R.success("数量修改成功！");
    }
}
